package org.wanho.rent.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import org.wanho.rent.constant.Constant;
import org.wanho.rent.entity.Company;
import org.wanho.rent.entity.ProductType;
import org.wanho.rent.entity.SysUser;
import org.wanho.rent.factory.ObjectFactory;
import org.wanho.rent.service.ProductTypeService;
import org.wanho.rent.util.AjaxResult;
import org.wanho.rent.util.PageInfo;
import org.wanho.rent.util.ResponseUtil;

/**
 * 产品类型控制器
 * @author student
 *
 */
@WebServlet(urlPatterns = "*.productType")
public class ProductTypeController extends HttpServlet {
	
	private ProductTypeService productTypeServiceProxy = (ProductTypeService) ObjectFactory.getObject("productTypeServiceProxy");
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println(request.getServletPath() + "###");
		String servletPath = request.getServletPath();
		if (servletPath.equals("/addproductType.productType")) {
			addProductType(request, response);
			System.out.println("-========+/productType");
		} else if (servletPath.equals("/selectAll.productType")) {
			selectAllProductTypeByPage(request, response);
		} else if (servletPath.equals("/updateProductType.productType")) {
			updateProductType(request, response);
		} else if (servletPath.equals("/updateProductTypeStatus.productType")) {
			updateProductTypeStatus(request, response);
		}

	}

	/**
	 * 修改产品类型状态
	 * @param request
	 * @param response
	 */
	private void updateProductTypeStatus(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String productTypeId = request.getParameter("productTypeId");
		String status = request.getParameter("status");

		System.out.println("productTypeId=" + productTypeId + " status= " + status);

		// 返回数据到浏览器
		ResponseUtil.doResponse("成功！", null, true, response);
	}

	/**
	 * 修改已有的产品类型信息
	 * @param request
	 * @param response
	 */
	private void updateProductType(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String productId = request.getParameter("productId");
		String productName = request.getParameter("productName");

		System.out.println("productId=" + productId + "## productName=" + productName);
		// 返回数据到浏览器
		ResponseUtil.doResponse("成功！", null, true, response);

	}

	/**
	 * 分页查询所有产品类型/查询所有
	 * @param request
	 * @param response
	 */
	private void selectAllProductTypeByPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String all = request.getParameter("all");
		// all不是null的时候就是查询所有，否则必须分页查询
		if (all != null) {

			List<ProductType> list = productTypeServiceProxy.findAll();
			// 返回数据到浏览器
			ResponseUtil.doResponse("成功！", list, true, response);
			return;

		} else {
			String pageNo = request.getParameter("pageNo");
			System.out.println(pageNo + "#");
			int page = Integer.parseInt(pageNo);

			PageInfo<ProductType> pageInfo = productTypeServiceProxy.findByPage(page, Constant.DEFAULT_PAGE_SIZE);

			ResponseUtil.doResponse("成功！",pageInfo, true,response);
		}

	}

	/**
	 * 添加产品类型
	 * @param request
	 * @param response
	 */
	private void addProductType(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String productTypeName = request.getParameter("productTypeName");
		System.out.println("productName=" + productTypeName);

		// 返回数据到浏览器
		ResponseUtil.doResponse("成功！", null, true, response);

	}
}
